package com.mtbu.bean;

import java.util.List;

import my.myorm.tool.jdbc.SQLExecutor;
import my.myorm.tool.jdbc.SQLManager;

import com.mtbu.bean.base.ProjectBase;
import com.mtbu.dao.DAOManager;
public final class Project extends ProjectBase {
	//获取频道加入项目后的第一个step
	public Step getChannelStepBegin() throws Exception {
		
		return getStepBegin().getNextStep();
	}
	
	//第一个step
	public Step getStepBegin() throws Exception {
		
		//return DAOManager.stepDao.findStepByProjectAndType(getId(), 2);
		//return DAOManager.stepDao.findStep("prestepid", "0","");
		return DAOManager.stepDao.findStepByProjectAndPrestepid(getId(), 0);
	}
	
	//第一个step
	public Step getStepEnd() throws Exception {
			
		//return DAOManager.stepDao.findStepByProjectAndType(getId(), 2);
		//return DAOManager.stepDao.findStep("prestepid", "0","");
		return DAOManager.stepDao.findStepByProjectAndNextstepid(getId(), 0);
	}
	
	public List<Step> getSteps() throws Exception {
		
		return DAOManager.stepDao.findStepByProject(getId());
	}
	
	
	public boolean isAllowDelChannel(SysUser usr) throws Exception{
		//这里要获得每个用户，有哪些project的,step的type=2的权限
		List<Project> projects =  DAOManager.projectDao.findChannelJoinAllowProjects(usr);
		
		for (Project project : projects) {
			if (project.getId() == this.getId()) {
				return true;
			}
		}
		return false;
	}
	
	public void createTableIfNotExist() throws Exception{
		if (getSplit().isEmpty() == false && getSplit().indexOf("__") == 0) {
			DAOManager.channelDao.createTableIfNotExist(getSplit());
		}
	}
	
	public String toLogString(){
		return "[" 
				+ this.getId() + ","
				+ this.getSplit() + ","
				+ this.getName() + "]";
	}
}
